/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.panes;


import com.eng.scc.core.view.ApplicationContext;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Rafael
 */
public class LoginPane extends JPanel
{

    public LoginPane()
    {
        initComponets();
    }
    
    private void clearFields()
    {
        tfName.setText( "" );
        tfPassword.setText( "" );
        lbInfo.setText( "" );

        tfName.requestFocus();
    }
    
    private void performLogin()
    {
        try
        {
            if ( tfName.getText() != null && !tfName.getText().isEmpty() )
            {
                if ( tfPassword.getPassword() != null && tfPassword.getPassword().length > 0 )
                {
                    lbInfo.setText( "" );

//                    boolean allow = DatabaseManagerService.getInstance().checkLogin( tfName.getText(), tfPassword.getPassword() );

                    if ( true )
                    {
                        fireEventListener();
                    }

                    else
                    {
                        lbInfo.setText( "Usuário não autorizado!" );
                    }

                }

                else
                {
                    lbInfo.setText( "Informe a senha!" );
                    tfPassword.requestFocus();
                }
            }

            else
            {
                lbInfo.setText( "Informe o login!" );
                tfName.requestFocus();
            }
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private void initComponets()
    {
        setLayout( new GridBagLayout() );
        
        lbName.setText( "Usuário:" );
        lbPassword.setText( "Senha:" );
        
        lbInfo.setForeground( Color.red );
        
        btOk.setText( "Entrar ");
        btClear.setText( "Limpar" );
        
        add( lbName, 
             new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, 
                                     GridBagConstraints.EAST, 
                                     GridBagConstraints.HORIZONTAL, 
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );

        add( tfName, 
             new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0, 
                                     GridBagConstraints.EAST, 
                                     GridBagConstraints.HORIZONTAL, 
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        add( lbPassword, 
             new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, 
                                     GridBagConstraints.EAST, 
                                     GridBagConstraints.HORIZONTAL, 
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        add( tfPassword, 
             new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0, 
                                     GridBagConstraints.EAST, 
                                     GridBagConstraints.HORIZONTAL, 
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        add( btOk, 
             new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, 
                                     GridBagConstraints.EAST, 
                                     GridBagConstraints.HORIZONTAL, 
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        add( btClear, 
             new GridBagConstraints( 1, 2, 1, 1, 0.0, 0.0, 
                                     GridBagConstraints.EAST, 
                                     GridBagConstraints.HORIZONTAL, 
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        
        lbInfo.setText( "" );
        
        add( lbInfo, 
             new GridBagConstraints( 0, 3, 2, 1, 0.0, 0.0, 
                                     GridBagConstraints.EAST, 
                                     GridBagConstraints.HORIZONTAL, 
                                     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
        
        btClear.addActionListener( new ActionListener() 
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                clearFields();
            }
        });
        
        btOk.addActionListener( new ActionListener() 
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                performLogin();
            }
        });

        tfPassword.addKeyListener( new KeyAdapter() 
        {
            @Override
            public void keyPressed( KeyEvent e )
            {
                if ( e.getKeyChar() == KeyEvent.VK_ENTER )
                {
                    performLogin();
                }
            }
        });
        
        
    }
    
    private JLabel lbName = new JLabel();
    private JLabel lbPassword = new JLabel();
    private JLabel lbInfo = new JLabel();
    
    private JTextField tfName = new JTextField();
    private JPasswordField tfPassword = new JPasswordField();
    
    private JButton btClear = new JButton();
    private JButton btOk = new JButton();

    private List<ChangeListener> listeners = null;
    
    public void addEventListener( ChangeListener changeListener )
    {
        if ( listeners == null )
        {
            listeners = new ArrayList<ChangeListener>();
        }
        
        listeners.add( changeListener );
    }
    
    private void fireEventListener()
    {
        if ( listeners != null )
        {
            for ( ChangeListener cl : listeners )
            {
                cl.stateChanged( null );
            }
        }
    }
}
